<template>
  <div class="login-container" :style="{backgroundImage:'url('+bgImg+')'}">
    <el-form ref="loginForm"
             :model="loginForm"
             :rules="loginRules"
             class="login-form"
             auto-complete="on"
             label-position="left">
      <div class="title-container">
        <h3 class="title">{{ config.login_title || '欢迎登录' }}</h3>
      </div>

      <el-form-item prop="login_name">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model.trim="loginForm.login_name"
                  :placeholder="$t('loginPage.fieldName')"
                  clearable
                  type="text" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input v-model.trim="loginForm.password"
                  type="password"
                  show-password
                  :placeholder="$t('loginPage.fieldPassword')"
                  @keyup.enter.native="handleLogin" />
      </el-form-item>
      <el-button :loading="loading"
                 type="primary"
                 style="width:100%;margin-bottom:20px;"
                 @click.native.prevent="handleLogin">Login</el-button>
      <div class="tips">
        <span style="margin-right:20px;">{{$t('loginPage.fieldName')}}: admin</span>
        <span>{{$t('loginPage.fieldPassword')}}: 111111</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'Login',
  computed: {
    ...mapGetters(['config']),
    bgImg () {
      return process.env.VUE_APP_WEB_URL + this.config.login_bg || require('@/assets/img/loginbg.jpeg')
    }
  },
  data () {
    return {
      loginForm: {
        login_name: 'admin',
        password: '111111'
      },
      loginRules: {
        login_name: [
          { required: true, trigger: 'blur', message: this.$t('loginPage.ruleName') }
        ],
        password: [
          { required: true, trigger: 'blur', message: this.$t('loginPage.rulePassword') }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin () {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            await this.$store.dispatch('user/login', this.loginForm)
            this.$message.success(this.$t('loginPage.loginSuccess'))
            const redirect = this.$route.query.redirect
            this.$router.push(redirect || '/')
          } catch (error) {
            console.log(error)
          } finally {
            //  不论成功失败都去关闭转圈
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 40px;
    width: 90%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 8px 5px 8px 10px;
      height: 40px;
    }
  }

  .el-form-item {
    border: 1px solid #ccc;
    border-radius: 0;
    color: #454545;
  }
}

.login-container {
  min-height: 100%;
  width: 100%;
  background-size:cover;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  align-items: center;

  .login-txt{
    position:absolute;
    right: -10px;
    top: 90px;
    width: 440px;
  }

  .login-form {
    position: relative;
    width: 440px;
    max-width: 100%;
    padding: 30px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
  }

  .tips {
    font-size: 14px;
    margin-bottom: 15px;
    color: #FF7F50;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 0 5px 0 15px;
    color: #777;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #FF7F50;
      margin: 0px auto 30px auto;
      padding-bottom: 10px;
      text-align: center;
      font-weight: normal;
      border-bottom: 1px solid #ddd;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
  }
}
</style>
